package cn.edu.nuc.zyb.frame;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import cn.edu.nuc.zyb.model.User;

//负责注册的服务器线程
public class UserAcceptRegist extends Thread {

	public Socket socket;
	public boolean flag = false;

	UserAcceptRegist(Socket socket) {
		this.socket = socket;
	}

	// 负责返回处理的结果
	public void send(boolean flag) {
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeBoolean(flag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		// 接收客户端发来的消息
		try {
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			String str = dis.readUTF();
			String[] msg = str.split(",");
			int password = Integer.valueOf(msg[1]);
			int age = Integer.valueOf(msg[3]);
			File file = new File("D:\\store\\user\\" + msg[0]);
			if (file.exists()) {
				send(flag);
				System.out.println("注册失败");
			} else {
				FileOutputStream fos = new FileOutputStream(file);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				User user = new User(msg[0], password, msg[2], age);
				oos.writeObject(user);
				this.flag = true;
				send(flag);
				System.out.println("注册成功");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
